package com.tangguangdi.base.db.generator.query;

import com.tangguangdi.base.common.db.FieldType;
import com.tangguangdi.base.db.generator.entity.FieldsQuery;
import com.tangguangdi.base.db.generator.entity.TablesQuery;

import java.util.Map;

public class MySqlQuery implements IDbQuery{

    @Override
    public TablesQuery tablesQuery() {
        TablesQuery query = new TablesQuery();
        query.setSql("show table status");
        query.setComment("Comment");
        query.setName("Name");
        query.setEngine("Engine");
        return query;
    }

    @Override
    public Boolean isView(Map map) {
        if(map.get("Comment").toString().equals("VIEW")) return true;
        return false;
    }

    @Override
    public FieldsQuery fieldsQuery() {
        FieldsQuery query = new FieldsQuery();
        query.setSql("show full fields from `%s`");
        query.setName("Field");
        query.setComment("Comment");
        return query;
    }

    @Override
    public Boolean isPrimaryKey(Map map) {
        if(map.get("Key").toString().equals("PRI")) return true;
        return false;
    }

    @Override
    public FieldType fieldType(Map map) {
        String type = map.get("Type").toString();
        if(type.contains("tinyint(1)")) return FieldType.BOOLEAN;
        if(type.contains("bit")) return FieldType.BOOLEAN;
        if(type.contains("bigint")) return FieldType.LONG;
        if(type.contains("int")) return FieldType.INTEGER;
        if(type.contains("decimal")) return FieldType.BIG_DECIMAL;
        if(type.contains("clob")) return FieldType.CLOB;
        if(type.contains("blob")) return FieldType.BLOB;
        if(type.contains("binary")) return FieldType.BYTE_ARRAY;
        if(type.contains("float")) return FieldType.FLOAT;
        if(type.contains("double")) return FieldType.DOUBLE;
        if(type.contains("datetime")) return FieldType.LOCAL_DATE_TIME;
        if(type.contains("date")) return FieldType.LOCAL_DATE;
        if(type.contains("time")) return FieldType.LOCAL_TIME;
        return FieldType.STRING;
    }
}
